package com.fivebrms.view.w9.validators
{
	import org.osflash.thunderbolt.Logger;
	
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.containers.VBox;
		
    import mx.validators.Validator;
    import mx.validators.ValidationResult;
	import mx.events.FlexEvent;
	
	import com.fivebrms.model.MainModelLocator;
	
	import com.fivebrms.view.w9.FormFields;
	
    public class W9FormValidator extends Validator 
    {
		private var formFields : FormFields;
		
		//
		// grab the vo from the model locator
		//
		public var model : MainModelLocator = MainModelLocator.getInstance();
        	
        private var results:Array;

        public function W9FormValidator( formFields : FormFields ) {
            super();

			//
			// pass a reference to the calling container with its public 
			// properties to this validator class
			//
			this.formFields = formFields;
			
			this.formFields.indivPartCont.enabled = false;
			this.formFields.corpBusCont.enabled = false;
        }
						
        override protected function doValidation(value : Object) : Array 
        {
        	//
            // Clear results Array.
            //
            results = [];
            
			//results = super.doValidation( value );
           
            Logger.info("w9 test..1.." + value );
            Logger.info("w9 test..1.." + value.id );
            Logger.info("w9 test..1.." + value.className );
            
            //
        	// check box group - unselect all check boxes
        	//
        	if 
        		( 
        			value.className  == "CheckBox"
        		)
        	{
        		this.formFields.chk_indivOrSolePropr.selected = false;
        		this.formFields.chk_partnership.selected = false;
        		this.formFields.chk_corporation.selected = false;
        		this.formFields.chk_business.selected = false;
        		
        		if 
        		(
        			( value.id == "chk_indivOrSolePropr" )
        		)
        		{
        			this.formFields.chk_indivOrSolePropr.selected = true;
					//
					// enable only the input fields being entered to be enabled
					//
					this.formFields.indivPartCont.enabled = true;
					this.formFields.corpBusCont.enabled = false;        			
        		}
        		
        		if 
        		(
        			( value.id == "chk_partnership" )
        		)
        		{
        			this.formFields.chk_partnership.selected = true;
 					//
					// enable only the input fields being entered to be enabled
					//
					this.formFields.indivPartCont.enabled = true;
					this.formFields.corpBusCont.enabled = false;   
				}
        		
        		if 
        		(
        			( value.id == "chk_corporation" )
        		)
        		{
        			this.formFields.chk_corporation.selected = true;
					//
					// enable only the input fields being entered to be enabled
					//
					this.formFields.indivPartCont.enabled = false;
					this.formFields.corpBusCont.enabled = true;  
        		}
        		
        		if 
        		(
        			( value.id == "chk_business" )
        		)
        		{
        			this.formFields.chk_business.selected = true;
        			//
					// enable only the input fields being entered to be enabled
					//
					this.formFields.indivPartCont.enabled = false;
					this.formFields.corpBusCont.enabled = true;  
        		}

       
        	}
            
        	//
        	// enable/disable checkbox and text input fields
        	//
        	if 
        		(
	        		( 
	        			value.className  == "CheckBox"
	        		)
	        		&&
	        		( 
	        			value.id == "chk_indivOrSolePropr" 
	        			||
	        			value.id == "chk_partnership" 
	        		)
	        	)	
        	{
				Logger.info("w9 test..3..");
				
				//
				// clear all other form fields
				//
				this.formFields.chk_corporation.selected = false;
				this.formFields.chk_business.selected = false;
				this.formFields.inp_corpBusBusinessName.text = "";
				this.formFields.inp_corpBusEmployIdentNumb.text = "";
				this.formFields.inp_corpBusStreetAddress.text = "";
        	}
	        			
        	//
        	// check box group - two
        	//
        	if 
        		(
	        		( 
	        			value.className  == "CheckBox"
	        		)
	        		&&
	        		( 
	        			value.id == "chk_corporation" 
	        			||
	        			value.id == "chk_business" 
	        		)
	        	)	
        	{
				Logger.info("w9 test..4..");
				
				//
				// clear all other form fields
				//
				this.formFields.chk_indivOrSolePropr.selected = false;
				this.formFields.chk_partnership.selected = false;
				this.formFields.inp_indivPartnName.text = "";
				this.formFields.inp_indivPartnSocialSecurityNumb.text = "";
        	}


			//
			// show warnings when not all form fields entered
			// 
			if
				(
					this.formFields.indivPartCont.enabled == true
				)
			{

				Logger.info("*** -1- ***");
				
				if
					(
						(this.formFields.inp_indivPartnName.text.length == 0)
					)
				{
					Logger.info("*** -2- ***");
					results.push(new ValidationResult(true, "inp_indivPartnName", "missing", "Required Field."));
				}
				
				if
					(
						(this.formFields.inp_indivPartnSocialSecurityNumb.text.length == 0)
					)
				{
					results.push(new ValidationResult(true, "inp_indivPartnSocialSecurityNumb", "missing", "Required Field."));
				}
				
				if
					(
						(this.formFields.inp_signature.text.length == 0)
					)
				{
					results.push(new ValidationResult(true, "inp_signature", "missing", "Required Field."));
				}
			}
			
			//
			// show warnings when not all form fields entered
			// 
			if
				(
					this.formFields.corpBusCont.enabled == true
				)
			{

				if
					(
						(this.formFields.inp_corpBusBusinessName.text.length == 0)
					)
				{
					results.push(new ValidationResult(true, "inp_corpBusBusinessName", "missing", "Required Field."));
				}
				
				if
					(
						(this.formFields.inp_corpBusEmployIdentNumb.text.length == 0)
					)
				{
					results.push(new ValidationResult(true, "inp_corpBusEmployIdentNumb", "missing", "Required Field."));
				}
				
				if
					(
						(this.formFields.inp_corpBusStreetAddress.text.length == 0)
					)
				{
					results.push(new ValidationResult(true, "inp_corpBusStreetAddress", "missing", "Required Field."));
				}
				
				if
					(
						(this.formFields.inp_signature.text.length == 0)
					)
				{
					results.push(new ValidationResult(true, "inp_signature", "missing", "Required Field."));
				}
			}
			
			Logger.info("w9 test..3..");
						
            return results;
        }
 
    }
}
